/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.sonar.model;

import java.util.Locale;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import org.hibernate.validator.Valid;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.NotEmpty;
import org.directwebremoting.annotations.RemoteProperty;
import org.internna.iwebjtracker.model.DefaultProject;
import org.internna.iwebmvc.metadata.EntitySecurity;
import org.internna.iwebmvc.metadata.EntitySecurity.CRUD;
import org.internna.iwebmvc.metadata.EntitySecurityRule;
import org.internna.iwebmvc.model.AbstractDomainEntity;
import org.internna.iwebjtracker.model.DefaultConnectionDetails;

/**
 * Stores Sonar server access data for a project.
 *
 * @author Jose Noheda
 * @since 1.0
 */
@Entity
@Table(name = "SONAR_PROJECTS")
@NamedQueries({
    @NamedQuery(name = "SonarConfiguration.findAll", query = "SELECT s FROM SonarConfiguration s"),
    @NamedQuery(name = "SonarConfiguration.findByProject", query = "SELECT s FROM SonarConfiguration s WHERE project.id = :project")
})
@EntitySecurity({
    @EntitySecurityRule(operation = CRUD.ALL, ifAnyGranted = {"ROLE_PROJECT_MANAGER"})
})
public class SonarConfiguration extends AbstractDomainEntity {

    private static final long serialVersionUID = -2876692327721941817L;

    /**
     * Named query to find the sonar configuration of a project.
     */
    public static final String FIND_BY_PROJECT = "SonarConfiguration.findByProject";

    /**
     * JQL statement to delete all previous Sonar configurations saved for a project.
     */
    public static final String DELELETE_SONAR_CONF_FOR_PROJECT = "DELETE FROM SonarConfiguration WHERE project.id = :project";

    @NotEmpty
    @Column(name = "MAVEN_RESOURCE")
    private String resource;

    @NotNull
    @JoinColumn(name = "PROJECT")
    @ManyToOne(fetch = FetchType.EAGER)
    private DefaultProject project;

    @Valid
    @RemoteProperty
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CONNECTION_DETAILS")
    private DefaultConnectionDetails connectionDetails;

    /**
     * Accessor method.
     *
     * @return any
     */
    public DefaultConnectionDetails getConnectionDetails() {
        return connectionDetails;
    }

    /**
     * Accessor method.
     *
     * @param connectionDetails any
     */
    public void setConnectionDetails(DefaultConnectionDetails connectionDetails) {
        this.connectionDetails = connectionDetails;
    }

    public DefaultProject getProject() {
        return project;
    }

    /**
     * Accessor method.
     *
     * @param project any
     */
    public void setProject(DefaultProject project) {
        this.project = project;
    }

    /**
     * Accessor method.
     *
     * @return any
     */
    public String getResource() {
        return resource;
    }

    /**
     * Accessor method.
     *
     * @param resource any
     */
    public void setResource(String resource) {
        this.resource = resource;
    }

    /**
     * Not supported.
     *
     * @param locale any
     */
    @Override public String getCaption(Locale locale) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
